package com.vsen.lpr.common.validated;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;

public class DateFormatValidator implements ConstraintValidator<DateFormat, String> {

    private DateFormat dateFormat;

    private final static String DATE_VALIDATED_STR = "yyyy-MM-dd";

    @Override
    public void initialize(DateFormat dateFormat) {
        this.dateFormat = dateFormat;
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        String format = dateFormat.format();
        if (DATE_VALIDATED_STR.equals(format)) {
            DateTimeFormatter dtf1 = new DateTimeFormatterBuilder()
                    .appendPattern(format)
                    .parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
                    .toFormatter();
            try {
                LocalDate.parse(s, dtf1);
            } catch (Exception e) {
                return false;
            }
        } else {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            try {
                simpleDateFormat.parse(s);
            } catch (Exception e) {
                return false;
            }
        }
        return true;
    }
}